from rest_framework import serializers

from question.models import Label,Question,Reply

from user.models import  User

#用户关注的标签序列化器
class ConcernSerializer(serializers.ModelSerializer):
    class Meta:
        model=Label
        fields=[
            "desc",
            "baike_url",
            "label_icon"
        ]

#标签列表序列化器
class LabelsSerializer(serializers.ModelSerializer):



    class Meta:
        model=Label
        fields=[
            "id",
            "label_name"
        ]


#回答的问题序列化器
class QuestionSerializer(serializers.ModelSerializer):

    id=serializers.IntegerField()
    user=serializers.StringRelatedField()
    labels=serializers.StringRelatedField(many=True)

    class Meta:
        model=Question

        fields=[
             "id",
             "createtime",
             "labels",
             "reply",
             "replyname",
             "replytime",
             "title",
             "unuseful_count",
             "useful_count",
             "user",
             "visits"]


#用户序列化器
class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ['id',
                  'username',
                  'avatar']

class ReplySerializerForSubAndParent(serializers.ModelSerializer):

    user = UserSerializer(read_only=True)
    class Meta:
        model=Reply
        fields = ["id", "content", "createtime", "useful_count", "unuseful_count", "user"]


#评论问题的序列化器
class ReplyQuestionSerialize(serializers.Serializer):



    user = UserSerializer(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)
    parent = ReplySerializerForSubAndParent(read_only=True)

    class Meta:
        model=Reply
        fields="__all__"